# match()函数是从内容的第一个字符开始匹配，如果匹配不到，就得到None
# search()函数从全部内容匹配，如果有多个，找到第一个匹配的
# findall()函数从全部内容匹配，如果有多个，找出所有匹配的
import re

from selenium import webdriver
import time
def gettimestamp():
    t = time.time()
    # print(t)  # 原始时间数据
    print(int(t))  # 秒级时间戳
    print(int(round(t * 1000)))  # 毫秒级时间戳
    # nowTime = lambda: int(round(t * 1000))  # 毫秒级时间戳步骤1，基于lambda
    # print(nowTime())  # 毫秒级时间戳步骤2，基于lambda
    return int(t)

# 折中方案，参数按如下方式组织，也是模拟multipart/form-data的核心

options = webdriver.FirefoxOptions()
options.add_argument('-headless')
driver = webdriver.Firefox(options=options)

def writetxt(msg):
    # print(request.META)
    print(msg)
    try:
        with open('postnum.txt', 'w+', encoding='utf-8') as f:
            f.write(msg + '\n')
    except:
        print('cuow')
        pass

def matchword(content,i):
    abc = re.compile(fr'href="/play/{i}/1/[0-9]*.html')
    myvalue = re.findall(abc, content)
    num = len(myvalue)
    print(num)
    if myvalue == None:
        # 丢弃
        return False
    else:
        #匹配到公众号
        return num-2

a = input("请输入开始视频编号，输入后按回车继续")
b = input("请输入结束视频编号，输入后按回车继续")
for i in range(int(b)):
        if i < int(a):
            continue
        writetxt(str(i))
        # print(f'目前进行到的视频数量是：{i}{s}')
        try:
            # https://www.35kan.com/vodplay/1715/1/1.html
            # url = f'https://www.koko6.com/play/{i}/1/1.html'
            # url = f'https://www.35kan.com/vodplay/{i}/1/1.html'
            # url = f'https://www.35kan.com/vodplay/{i}/1/1.html'
            # url = f'https://www.lai4.com/play/{i}-1-1.html'
            # url = f'https://www.qtmovie.com/play/{i}-1-1.html'
            url = f'http://www.winppo.com/vod/play/id/{i}/sid/1/nid/1.html'

            print(url)
            driver.get(url=url)
            # content = driver.page_source
            #查找是否有续集
            # if matchword(content, i) == False:
            #     continue
            # else:
            #     num = matchword(content, i)
            #     for s in range(num):
            #         if s == 0 or 1:
            #             continue
            #         url = f'https://www.koko6.com/play/{i}/1/{s}.html'
            #         print(url)
            #         driver.get(url=url)
            #         content = driver.page_source
        except:
            print(f"第{i}条请求失败")
            continue
        # print(content)
        try:
            # driver.implicitly_wait(1)


            driver.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[3]/ul[1]/li[2]/input').send_keys("站长送你一个免费领淘宝优惠券还能返钱的机器人，加微信直接领: buy3600")

            driver.find_element_by_xpath('/html/body/div[3]/div/div[3]/div[3]/ul[1]/li[3]/a').click()

            driver.delete_all_cookies()
        except:
            print(f"第{i}条发送失败")
            # time.sleep(1)
            continue
driver.close()